/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年6月27日
 */
package com.massyframework.beanskin.runtime.nativelib;

import com.massyframework.beanskin.ModuleListener;
import com.massyframework.beanskin.customer.Customer;
import com.massyframework.beanskin.framework.FrameworkConfig;
import com.massyframework.beanskin.framework.FrameworkListener;
import com.massyframework.beanskin.launching.LaunchingHandler;
import com.massyframework.beanskin.runtime.nativelib.script.ScriptEngineFactoryServiceFactory;
import com.massyframework.beanskin.service.crypto.RSAService;
import com.massyframework.beanskin.service.script.ScriptEngineFactory;
import com.massyframework.beanskin.util.localstore.LocalStore;
import com.massyframework.beanskin.util.sorting.Order;

/**
 * 原生代码初始化器
 * @author huangkh
 *
 */
@Order(0)
public class NativeLibraryInitializer implements LaunchingHandler {

	/**
	 * 
	 */
	public NativeLibraryInitializer() {
	}

	@Override
	public void init(FrameworkConfig config) {	
		LocalStore localStore = config.getBean(LocalStore.class.getName(), LocalStore.class);
		NativeLibrary library = new NativeLibrary("secure", localStore);
		library.load();
		
		Customer customer = config.getBean(Customer.class.getName(), Customer.class);
		
		//注册NativeRASService
		NativeRSAService rasService = new NativeRSAService();
		config.defineExportService(NativeRSAService.class.getName(), rasService, builder -> {
			builder.addExportService(RSAService.class)
				.name("native")
				.topRanking();
		});
		
		//注册ScriptEngineFactory服务
		config.defineExportService(ScriptEngineFactory.class.getName(), new ScriptEngineFactoryServiceFactory(), builder->{
			builder.addExportService(ScriptEngineFactory.class)
				.isFactory(true)
				.topRanking();
		});
		
		ModuleMonitor monitor = new ModuleMonitor(library, localStore, customer);
		config.addListener((FrameworkListener)monitor);
		config.addListener((ModuleListener)monitor);
	}
	
}
